package com.ecoolex.chinadutyfree.retailer.admin.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.admin.core.AdminConstant;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsProduct;
import com.ecoolex.chinadutyfree.retailer.entity.TaxRate;
import com.ecoolex.chinadutyfree.retailer.entity.TaxRate.Column;
import com.ecoolex.chinadutyfree.retailer.pojo.AddTaxRateDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.EditTaxRateDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.QueryTaxRateDTO;
import com.ecoolex.chinadutyfree.retailer.ws.TaxRateService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.google.common.collect.Lists;

@GraphQL2Module(type = Type.Admin, description = "税率管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = TaxRateGraphql.AUTH, name = "税率管理")
public class TaxRateGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-TAX-RATE";
	
	@Reference
	private TaxRateService taxRateService;
	
	@TypeField(type = DirectMailGoodsProduct.class,field = "taxRate",description = "税率分类")
	public TaxRate taxRate(@Source DirectMailGoodsProduct source) {
		return taxRateService.getTaxRate(source.getTaxRateId());
	}
	
	@TypeField(type = TaxRate.class,field = "lastTaxRate",description = "上级税率分类")
	public TaxRate lastTaxRate(@Source TaxRate source) {
		return taxRateService.getTaxRate(source.getLastTaxRateId());
	}
	
	@TypeField(type = TaxRate.class,field = "childrenTaxRate",description = "下级税率分类")
	public List<TaxRate> childrenTaxRate(@Source TaxRate source) {
		return taxRateService.listByLastTaxRateId(source.getTaxRateId());
	}
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	@QueryField(field = "taxRate", description = "税率分类详情")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "税率分类详情")
	public TaxRate get(@NonNull Integer taxRateId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return taxRateService.getTaxRate(taxRateId);
	}
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@QueryField(field = "taxRateList", description = "税率分类列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "税率分类列表")
	public Pagination<TaxRate> query(@NonNull QueryTaxRateDTO query,@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user){
		user.assertAuth(AUTH_QUERY);
		return taxRateService.queryTaxRate(query, sorts, page);
	}
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	@MutationField(field = "addTaxRate", description = "添加税率分类")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加税率分类")
	public TaxRate add(@NonNull AddTaxRateDTO add,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return taxRateService.addTaxRate(add, user.userName());
	}
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	@MutationField(field = "editTaxRate", description = "编辑税率分类")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑税率分类")
	public TaxRate edit(@NonNull EditTaxRateDTO edit,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		TaxRate old = taxRateService.getTaxRate(edit.getTaxRateId());
		BizAssert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "税率分类不存在");
		BeanUtil.copy(edit,
			old,
			Lists.newArrayList(Column.level,
				Column.lastTaxRateId,
				Column.type,
				Column.deleted,
				Column.createUser,
				Column.createTime).stream().map(Column::getJavaProperty).toArray(String[]::new));
		return taxRateService.editTaxRate(old, user.userName());
	}
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	@MutationField(field = "delTaxRate", description = "删除税率分类")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除税率分类")
	public Boolean delete(@NonNull Integer taxRateId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		TaxRate old = taxRateService.getTaxRate(taxRateId);
		BizAssert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "税率分类不存在");
		taxRateService.deleteTaxRate(old, user.userName());
		return true;
	}
}
